<script setup lang="ts">
//
import XtxSwiper from '@/components/XtxSwiper.vue'
import XtxGuess from '@/components/XtxGuess.vue'
import CustomNavbar from './components/CustomNavbar.vue'
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import skeletonPanel from './components/skeletonPanel.vue'
import { useGuessList } from '@/composables'

const BannerList = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  BannerList.value = res.result
}
const categoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}

const HotPanelList = ref<HotItem[]>([])
const getHomeHotData = async () => {
  const res = await getHomeHotAPI()
  HotPanelList.value = res.result
  console.log(HotPanelList.value)
}
const { guessRef, onScrolltolower } = useGuessList()
const resetGuessData = () => {
  guessRef.value?.resetData()
}
const isloading = ref(false)
onLoad(async () => {
  isloading.value = true
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotData()])
  isloading.value = false
})
const isRefresh = ref(false)
const onRefresherrefresh = async () => {
  isRefresh.value = true
  resetGuessData()
  await Promise.all([
    getHomeBannerData(),
    getHomeCategoryData(),
    getHomeHotData(),
    guessRef.value?.getMore(),
  ])
  isRefresh.value = false
}
</script>

<template>
  <!-- 顶部搜索 -->
  <custom-navbar />

  <scroll-view
    refresher-enabled
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isRefresh"
    class="scroll-view"
    @scrolltolower="onScrolltolower"
    scroll-y
  >
    <skeletonPanel v-if="isloading" />
    <!-- 轮播图 -->
    <template v-else>
      <xtx-swiper :list="BannerList" />
      <!-- 分类 -->
      <category-panel :list="categoryList" />
      <!-- 热门推荐 -->
      <hot-panel :list="HotPanelList" />
      <!-- 猜你喜欢 -->
      <XtxGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
//底部颜色
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
